import { request } from "../../request/index.js"

Page({
  data: {
    tabs: [
      {
        id: 0,
        name: "综合",
        isActive: true
      },
      {
        id: 1,
        name: "销量",
        isActive: false
      },
      {
        id: 2,
        name: "价格",
        isActive: false
      }
    ],
    goodsList: []
  },
  queryParams: {
    query: '',
    cid: '',
    pagenum: 1,
    pagesize: 10,
  },
  total: '',
  onLoad: function (options) {
    this.queryParams.cid = options.cid
    this.getgoodsList()
  },
  handletabtap(e) {
    let { tabs } = this.data
    tabs.forEach((v, i) => {
      i === e.detail.index ? v.isActive = true : v.isActive = false
    })
    this.setData({
      tabs
    })
  },
  async getgoodsList() {
    const res = await request({
      url: "goods/search", data: this.queryParams
    })
    this.setData({
      goodsList: [...this.data.goodsList, ...res.data.message.goods]
    })
    this.total = res.data.message.total
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    const totalpage = Math.ceil(this.total / this.queryParams.pagesize)
    if (this.queryParams.pagenum >= totalpage) {
      wx.showToast({
        title: '没有数据了',
      });

    } else {
      this.queryParams.pagenum++
      this.getgoodsList()
    }
  },
  onPullDownRefresh() {
    this.setData({
      goodsList: []
    })
    this.queryParams.pagenum = 1,
      this.getgoodsList()


  }
})